package proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;

/**
 * @author :spj
 * @Date:2022/3/28
 * @description:
 */
public class CGLIB {
    public Object createProxyObject(Object obj) {
        Object o = Enhancer.create(obj.getClass(), new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                //前置方法
                System.out.println("cglib前置");
                //这种写法不用去记忆
                Object o1 = methodProxy.invokeSuper(o, objects);
                //后置方法
                System.out.println("cglib后置");
                return o1;
            }
        });
        return o;
    }
}
